package system.dao;

import com.jfinal.kit.JsonKit;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import core.shiro.ShiroUtils;
import core.toolbox.Period.PeriodUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by Administrator on 2017/3/20.
 */
public class SubjectBalanceDao {

    protected final Log logger = Log.getLog(getClass());

    /**
     * 在原来的基础上加上数据
     * @param r
     */
    public boolean updateDetailForOccurredBalance(Record r) {
        return Db.update("update user_occurred_balance " +
                        "set debit_balance = debit_balance + ?, credit_balance = credit_balance + ? " +
                        "where user_id = ? and account_period = ? and subject_id = ?",
                r.get("voucher_debit"),
                r.get("voucher_credit"),
                r.get("user_id"),
                r.get("account_period"),
                r.get("subject_id")
                ) == 1;


    }

    /**
     * 累加数据在周期为0的数据上
     * @param r
     */
    public boolean updateDetailForAccumulatedBalance(Record r) {

        return Db.update("update user_accumulative_balance " +
                        "set debit_balance = debit_balance + ?, credit_balance = credit_balance + ? " +
                        "where user_id = ? and account_period = ? and subject_id = ?",
                r.get("voucher_debit"),
                r.get("voucher_credit"),
                r.get("user_id"),
                0,
                r.get("subject_id")
        ) == 1;


    }

    /**
     * 确保数据库中已经有此用户,此周期,此会计科目
     * @param r
     */
    public void sureBalanceSubject(Record r) {
        Record t = new Record();
        t.setColumns(r);
        List<Record> records1 = Db.find("select * from user_occurred_balance where user_id = ? and account_period = ? and subject_id = ?",
                t.get("user_id"),
                t.get("account_period"),
                t.get("subject_id")
        );
        List<Record> records2 = Db.find("select * from user_accumulative_balance where user_id = ? and account_period = ? and subject_id = ?",
                t.get("user_id"),
                0,
                t.get("subject_id")
        );
        if(records1.size() == 0 || records2.size() == 0) {
            t.remove("voucher_debit").remove("voucher_credit");
            if(records1.size() == 0){
                Db.save("user_occurred_balance", t);
            }
            if(records2.size() == 0){
                Db.save("user_accumulative_balance", t.set("account_period", 0));
            }
        }
    }

    /**
     * 当前发生余额中去除记录 累计余额中去除记录
     * @param detail
     */
    public boolean removeDetailForBalance(Record detail) {
        removeDetailSolveBigDecimal(detail);
        boolean b1 = updateDetailForOccurredBalance(detail);
        boolean b2 = updateDetailForAccumulatedBalance(detail);
        if(!b1)
            logger.error("删除发生余额记录失败 " + JsonKit.toJson(detail));
        if(!b2)
            logger.error("删除累计余额记录失败 " + JsonKit.toJson(detail));
        return  b1 && b2;
    }

    /**
     * 将detail中的voucher_debit和voucher_credit变换成负数
     * @param detail
     */
    private void removeDetailSolveBigDecimal(Record detail){
        BigDecimal voucher_debit = detail.get("voucher_debit");
        BigDecimal voucher_credit = detail.get("voucher_credit");
        detail.set("voucher_debit", voucher_debit.multiply(new BigDecimal(-1)));
        detail.set("voucher_credit", voucher_credit.multiply(new BigDecimal(-1)));
    }

    /**
     * 根据周期获取用户的周期累计余额记录
     * @param period
     */
    public List<Record> getUserSubjectAccumulatedBalanceByPeriod(int user_id, int period) {
        return Db.find("select subject_id, " +
                        "(select subject_name from account_subject b where b.subject_id = a.subject_id) subject_name, " +
                        "debit_balance, " +
                        "credit_balance " +
                        "from user_accumulative_balance a " +
                        "where user_id = ? and account_period = ? ",
                user_id,
                period == PeriodUtils.getCurrentPeriod()+1?0:period);


    }

    public List<Record> getUserSubjectOccurredBalanceByPeriod(int user_id, int period) {
        return Db.find("select subject_id, " +
                "(select subject_name from account_subject b where b.subject_id = a.subject_id) subject_name, " +
                "debit_balance, " +
                "credit_balance " +
                "from user_occurred_balance a " +
                "where user_id = ? and account_period = ?", user_id, period);
    }
}
